<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    class Node {
        constructor(e, next) {
            this.e = e;
            this.next = next
        }
    }

    // 链表
    class LinkedList {
        constructor() {
            this.dummyHead = new Node();
            this.size = 0;
        }

        add(index, e) {
            if (index < 0 || index > this.size) {
                throw new Error("Add failed. Illegal index")
            }

            let prev = this.dummyHead;

            for (let i = 0; i < index; i++) {
                prev = prev.next
            }

            prev.next = new Node(e, prev.next);
            this.size++
        }

        addFirst(e) {
            this.add(0, e);
        }

        addLast(e) {
            this.add(this.size, e)
        }

        get(index) {
            if (index < 0 || index > this.size) {
                throw new Error("Add failed. Illegal index")
            }

            let cur = this.dummyHead.next;
            for (let i = 0; i < index; i++) {
                cur = cur.next
            }

            return cur.e
        }

        getFirst() {
            return this.get(0)
        }

        getLast() {
            return this.get(this.size - 1)
        }

        set(index, e) {
            if (index < 0 || index > this.size) {
                throw new Error("Add failed. Illegal index")
            }

            let cur = this.dummyHead.next;
            for (let i = 0; i < index; i++) {
                cur = cur.next
            }
            cur.e = e
        }

        contains(e) {
            let cur = this.dummyHead.next;
            while (cur !== null) {
                if (cur.e === e) {
                    return true
                }
                cur = cur.next
            }
            return false
        }

        remove(index) {
            if (index < 0 || index > this.size) {
                throw new Error("Add failed. Illegal index")
            }

            let prev = this.dummyHead;

            for (let i = 0; i < index; i++) {
                prev = prev.next
            }

            let delNode = prev.next;
            prev.next = delNode.next;
            delNode.next = null;
            this.size--
        }

        removeFirst() {
            this.remove(0)
        }

        removeLast() {
            this.remove(this.size - 1)
        }

        removeElement(el) {
            let prev = this.dummyHead;

            while (prev.next !== null) {
                if (prev.next.e === el) {
                    prev.next = prev.next.next
                } else {
                    prev = prev.next
                }
            }

            this.size--
        }

        toString() {
            let ret = '',
                cur = this.dummyHead.next;
            while (cur) {
                ret += `${cur}->`;
                cur = cur.next
            }
            ret += 'null';
            return ret
        }
    }


</script>
</body>
</html>